package com.ruoyi.business.controller;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import com.ruoyi.business.service.IProcessService;
import com.ruoyi.business.vo.DefinitionDetailVO;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.file.FileUploadUtils;
import org.apache.commons.io.IOUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.xmlbeans.impl.common.IOUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.business.domain.DefinitionDetail;
import com.ruoyi.business.service.IDefinitionDetailService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

/**
 * 流程定义明细Controller
 * 
 * @author zw
 * @date 2021-10-13
 */
@Controller
@RequestMapping("/business/definitionDetail")
public class DefinitionDetailController extends BaseController
{
    private String prefix = "business/definitionDetail";

    @Autowired
    private IDefinitionDetailService definitionDetailService;

    @Autowired
    private IProcessService processService;

    @RequiresPermissions("business:definitionDetail:view")
    @GetMapping("/{definitionInfoId}")
    public String definitionDetail(@PathVariable("definitionInfoId") Long definitionInfoId,ModelMap map)
    {
        map.put("definitionInfoId", definitionInfoId);
        return prefix + "/definitionDetail";
    }

    /**
     * 查询流程定义明细列表
     */
    @RequiresPermissions("business:definitionDetail:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(DefinitionDetail definitionDetail)
    {
        startPage();
        List<DefinitionDetailVO> list = definitionDetailService.selectDefinitionDetailList(definitionDetail);
        return getDataTable(list);
    }
    /**
     * 读取xml文件或者img文件
     */
    @RequiresPermissions("business:definitionDetail:readResource")
    @GetMapping("/readResource")
    @ResponseBody
    public void readResource(String type, Long deploymentId, HttpServletResponse resp) throws IOException {
        InputStream inputStream = null;
        if ("xml".equals(type)){
            inputStream = processService.getFileAsXMLAsStreamByDeploymentId(deploymentId);
        }else if ("img".equals(type)){
            inputStream = processService.GenerateImgAsStreamByDeploymentId(deploymentId);
        }
        IOUtils.copy(inputStream, resp.getOutputStream());
    }

    /**
     * 导出流程定义明细列表
     */
    @RequiresPermissions("business:definitionDetail:export")
    @Log(title = "流程定义明细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(DefinitionDetail definitionDetail)
    {
        List<DefinitionDetailVO> list = definitionDetailService.selectDefinitionDetailList(definitionDetail);
        ExcelUtil<DefinitionDetailVO> util = new ExcelUtil<>(DefinitionDetailVO.class);
        return util.exportExcel(list, "流程定义明细数据");
    }

    /**
     * 导出流程定义明细上传
     */
    @RequiresPermissions("business:definitionDetail:deploy")
    @Log(title = "流程定义明细部署", businessType = BusinessType.OTHER)
    @PostMapping("/deploy")
    @ResponseBody
    public AjaxResult deploy(@RequestParam("processDefinition")MultipartFile file, Long definitionInfoId) {
        if (definitionInfoId == null){
            return AjaxResult.error("请先选择流程定义再部署文件");
        }
        //判空
        if(file != null){
            String filename = file.getOriginalFilename();
            assert filename != null;
            String fileType = filename.substring(filename.lastIndexOf(".") + 1);
            if (!("bpmn".equals(fileType) || "zip".equals(fileType))){
                return AjaxResult.error("文件格式有误,请重新上传!");
            }else {
                try {
                    //资源下载路径
                    String uploadPath = RuoYiConfig.getUploadPath();
                    //文件保存路径
                    String filePath = FileUploadUtils.upload(uploadPath, file);
                    //文件保存真实路径(全部)
                    filePath = RuoYiConfig.getProfile() + filePath.substring(Constants.RESOURCE_PREFIX.length());
                    //部署流程
                    definitionDetailService.deployProcessDefinition(definitionInfoId, filePath);
                    return AjaxResult.success("上传成功");
                } catch (IOException e) {
                    e.printStackTrace();
                    return AjaxResult.error("部署失败,请联系管理员!");
                }
            }
        }else {
            return AjaxResult.error("上传文件不能为空!");
        }
    }
}
